﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using LitJson;
using ProtoBuf;

public class LoginPanel : MonoBehaviour
{
    public InputField inputField;
    string inputName = "";

    void Awake()
    {
        EventManager.loginSuccess += OnLogin;
    }

    private void OnDestroy()
    {
        EventManager.loginSuccess -= OnLogin;
    }

    /// <summary>
    /// 登录按钮
    /// </summary>
    public void OnClick()
    {
        //封装用户登录数据
        string username = inputField.text;
        Color color = new Color(Random.value, Random.value, Random.value);
        UserData data = new UserData(username,color);

        inputName = username;

        //发送登录消息
        string json = JsonUtility.ToJson(data);
        NetworkManager.Instance.SendMessage(Protocol.Login_CMD, json);
    }

    //登录成功回调，接到消息隐藏页面
    void OnLogin(string json,bool islocal)
    {
        gameObject.SetActive(false); 
    }
}
